package com.jd.fruit.model.admin.auth;

import java.util.ArrayList;
import java.util.List;

import com.jd.fruit.common.annotation.Table;
import com.jd.fruit.common.model.BaseModel;
import com.jd.fruit.common.util.StringUtil;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

/**
 * 角色
 * 
 */
@SuppressWarnings("serial")
@Table("t_admin")
public class AdminUser extends BaseModel<AdminUser> { 
	public static final AdminUser dao = new AdminUser();
	protected List<Object> whereParams = new ArrayList<Object>();	
	/**
	 * 保存管理员
	 * @param user
	 */
	public String save(AdminUser adminUser){
		try {
			String currntId=StringUtil.getKey();
			adminUser.set("id",currntId);
			adminUser.save();
			return currntId;
		} catch (Exception e) {
			throw new RuntimeException("管理员保存异常！", e);
		}
	}
	
	public Page<AdminUser> list(int pageNumber,int pageSize){ 
		whereParams.clear();
		StringBuffer whereSql=new StringBuffer("FROM `t_admin` a  where 1=1 ");
		return this.paginate(pageNumber, pageSize,"SELECT * " ,whereSql.toString(),whereParams.toArray());
	}
	

	
	@Override
	public void initWhere() {
		if(StrKit.notBlank(this.getStr("name"))){
			appendSql("AND", "a.name", "like", "%"+getStr("name")+"%");
		}
	}
	/**
	 * 
	 * @param loginName
	 * @param password
	 */
	public AdminUser checkLogin(String loginName, String password) {
	   String sql="SELECT * FROM `t_admin`  t WHERE t.login_name =? AND t.password=?";
	   return this.findFirst(sql, loginName,password);
	}
	
	
}
